package com.ray.web.controller;


import com.ray.common.utils.*;
import com.ray.web.entity.Menu;
import com.ray.web.entity.TreeNode;
import com.ray.web.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author ray
 * @since 2020-12-04
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @GetMapping("/toLeftMenuJson")
    public Result toLeftMenuJson(){
        //查询菜单
        List<Menu> list = menuService.list();
        List<TreeNode> nodes = new ArrayList<>();
        for (Menu menu : list) {
            Integer id = menu.getId();
            Integer parentId = menu.getParentId();
            String title = menu.getTitle();
            String icon = menu.getIcon();
            String href = menu.getHref();
            Boolean spread = menu.getSpread()== Constast.SPREAD_TRUE?true:false;
            nodes.add(new TreeNode( id, parentId, title, icon, href, spread));
        }
        //变为层级关系
        List<TreeNode> buildNodes = TreeNodeBuilder.build(nodes, 1);
        return Result.success(buildNodes);
    }




}
